package com.hqyj.javaSpringCloudAccount.dao;

import com.hqyj.entity.account.Resource;
import com.hqyj.entity.common.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ResourceDao {

    @Select("select * from account_resource where id = #{id}")
    @Results(id="resourceResult", value={
            @Result(column="id", property="id"),
            @Result(column="id",property="roles",
                    javaType=List.class,
                    many=@Many(select="com.hqyj.javaSpringCloudAccount.dao.RoleDao.getRolesByResourceId"))
    })
    Resource getResourceById(int id);

    @Select("<script>"
            + "select * from account_resource "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (permit like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Resource> getResourceByVo(SearchBean searchBean);

    @Insert("insert into account_resource (create_date, permit) " +
            "values(#{createDate}, #{permit})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertResource(Resource resource);

    @Update("update account_resource set permit = #{permit} where id = #{id}")
    void updateResource(Resource resource);

    @Delete("delete from account_resource where id = #{id}")
    void deleteResourceById(int id);

    @Select("select * from account_resource ar left join account_role_resource arr on ar.id = arr.resource_id " +
            "where arr.role_id = #{roleId}")
    List<Resource> getResourcesByRoleId(Integer roleId);
}
